package org.example.strategypatterndemo.module1.controller;

import org.example.strategypatterndemo.module1.PromotionType;
import org.example.strategypatterndemo.module1.service.impl.DiscountStrategy;
import org.example.strategypatterndemo.module1.service.impl.FullReduceStrategy;
import org.example.strategypatterndemo.module1.service.impl.GiftStrategy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * RefactoredTestController
 * @author dingchen
 * @version 2025/05/29 16:48
**/
@RestController
public class SimpleTestController {

    /**
     * 测试接口（硬编码实现促销策略）
     * http://localhost:8080/simpleCalculate?orderAmount=300&promotionType=FULL_REDUCE
     */
    @GetMapping("/simpleCalculate")
    public String simpleCalculatePrice(@RequestParam double orderAmount, @RequestParam PromotionType promotionType) {
        double finalPrice;

        // 硬编码创建策略实例
        FullReduceStrategy fullReduceStrategy = new FullReduceStrategy();
        DiscountStrategy discountStrategy = new DiscountStrategy();
        GiftStrategy giftStrategy = new GiftStrategy();

        // 根据促销类型调用相应策略的计算方法
        switch (promotionType) {
            case FULL_REDUCE: // 满减策略（满200减50）
                double fullReduceDiscount = fullReduceStrategy.calculateDiscount(orderAmount);
                finalPrice = orderAmount - fullReduceDiscount;
                break;
            case DISCOUNT:    // 打折策略（打8折）
                double discount = discountStrategy.calculateDiscount(orderAmount);
                finalPrice = orderAmount - discount;
                break;
            case GIFT:        // 赠品策略（无金额优惠）
                giftStrategy.calculateDiscount(orderAmount);
                finalPrice = orderAmount;
                break;
            default:
                throw new IllegalArgumentException("未知促销类型：" + promotionType);
        }

        return "订单金额：" + orderAmount + "元，使用" + promotionType.getDesc() + "后，最终价格：" + finalPrice + "元";
    }
}
